package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper,AdChannel> implements AdChannelService{

    /**
     * 根据名称分页查询频道列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {

        //1.参数检测
        if (dto==null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        //2.按名称模糊分页查询
        Page page = new Page(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdChannel::getName,dto.getName());
        }
        IPage result = page(page, wrapper);

        //3.结果封装
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }


    /*
    * 新增
    * */
    @Override
    public ResponseResult insert(AdChannel adchannel) {
        //1.参数校验
        if (adchannel == null || StringUtils.isBlank(adchannel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //2.执行新增
        adchannel.setCreatedTime(new Date());
        if (adchannel.getIsDefault()){
            LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(wrapper);

            //判断是否存在默认频道
        if (one != null){
            one.setIsDefault(false);
            updateById(one);
        }
    }
    save(adchannel);
    //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
    * 删除
    * */
    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断当前参数是否存在，是否有效
        AdChannel channel = getById(id);
        if (channel == null){
            throw new CustomException(AppHttpCodeEnum.ADCHANNEL_DATA_NOT_EXIST_ERROR);
        }
        //是否为默认
        if (channel.getIsDefault()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISDEFAULT_INVALID);
        }
        //状态是否启动
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    /*
    * 修改
    * */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //检查参数
        if (adChannel == null || adChannel.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查后改
        AdChannel channel = getById(adChannel.getId());
        if (channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
